// app.js
const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
// const models = require('./models');
const userModel = require('./models/userModel');
const app = new Koa();
const router = new Router();


require('./models/db');
// 使用bodyParser中间件解析请求体
app.use(bodyParser());

// 定义路由
router.get('/', async (ctx) => {
  ctx.body = 'Welcome to Koa with Sequelize!';
});

// 查找所有用户
router.get('/users', async (ctx) => {
  const users = await userModel.findAll();
  let data = await redis.get(users);
  if(!data){
    const users = await userModel.findAll();
    data=users
    await redis.set(key, data, 'EX', 6000); // 缓存有效期为6000秒
  }
  ctx.body = data;
});


// 使用路由中间件
app.use(router.routes()).use(router.allowedMethods());

app.listen(3000, () => {
  console.log('Server is running at http://localhost:3000');
});